package com.leetcode.problems029;


/**
 * https://leetcode-cn.com/problems/divide-two-integers/
 * @Date 2021/11/20 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {


    public int divide(int dividend, int divisor) {
        if(dividend == divisor){
            return 1;
        }
        if(divisor == Integer.MIN_VALUE){
            return 0;
        }
        boolean isNegative = false;
        if(dividend < 0 && divisor < 0){
            isNegative = false;
        }else if(dividend < 0 || divisor < 0){
            isNegative = true;
        }
        int bl = 0;
        if(dividend == Integer.MIN_VALUE){
            bl = 1;
            dividend = dividend - bl;
        }
        dividend = Math.abs(dividend);
        divisor = Math.abs(divisor);
        if(dividend < divisor){
            return 0;
        }
        int res = 0;
        if(divisor == 1){
            if(bl != 0 && dividend == Integer.MAX_VALUE){
                return isNegative?Integer.MIN_VALUE:Integer.MAX_VALUE;
            }
            return isNegative? -dividend : dividend;
        }

        while(dividend >= divisor){
            dividend = dividend - divisor;
            dividend += bl;
            bl = 0;
            res++;
        }


        return isNegative ? -res : res;
    }


    public static void main(String[] args) {
        System.out.println(new Solution().divide(Integer.MIN_VALUE,-1));
    }
}
